<template>
  <BaseMainSlot>
    <template v-slot:btnsBar></template>
    <template v-slot:treebox>
      <typeTree
        @queryDataBytype="queryDataBytype"
        @updataTable="queryPageList"
        @updataTable1="updataTable1"
        :page-no="139"
      />
    </template>
    <template v-slot:searchbox>
      <div class="searchbox">
        <div class="select-box-content">
          <div class="select-box-content-name">型号名称：</div>
          <el-input
            v-model="modelName"
            clearable
            placeholder="请输入型号名称"
            @change="queryPageList()"
          />
        </div>
        <ActionBar
          :add-active="addActive"
          :del-active="delActive"
          :has-edit="false"
          :has-cancel="false"
          :has-save="false"
          :page-no="138"
          @add="add"
          @del="del"
        />
      </div>
    </template>
    <template v-slot:tablebox>
      <e-table
        ref="tableData"
        rowKey="equModelCode"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @clickButton="clickButton"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="operate"
          v-if="scoped.row.equModelCode"
          slot-scope="scoped"
        >
          <span
            class="table-btns-box"
            @click="edit(scoped.row)"
          >
            <i class="el-icon-edit-outline"></i>
            <span>修改</span>
          </span>

          <span
            v-if="judgePermission(139,'deleteone')"
            class="table-btns-box table_del_btn"
            @click="dels(scoped.row)"
          >
            <i class="el-icon-delete"></i>
            <span>删除</span>
          </span>

          <span
            class="table-btns-box"
            @click="queryDetail(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>
      </e-table>

      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancelDialog"
      />
      <ShowDetailDialog
        ref="ShowDetailDialog"
        :equ-model-template-list="equModelTemplateList"
        :show.sync="showDetailDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancelDialog"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import equipTemplateApi from "@/api/equip-manage/equip-template";
import BaseMainSlot from "@/components/BaseMainSlot/tree-type.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import ShowDetailDialog from "./components/show-detail-dialog.vue";
import { mymixin } from "@/utils/mixin";
// import equipTypeApi from "@/api/equip-manage/equip-type/index";
import equipModelApi from "@/api/equip-manage/equip-model/index";
import typeTree from "./components/type-tree.vue";
import { mapGetters } from "vuex";

export default {
  name: "EquipTemplate",
  components: {
    ActionBar,
    BaseMainSlot,
    AddEditDialog,
    ShowDetailDialog,
    typeTree
  },
  mixins: [mymixin],
  data() {
    return {
      // 控制弹出框显示隐藏
      showAddEditDialog: false,
      showDetailDialog: false,
      // 搜索条件:型号名称：
      modelName: "",
      checkedIdList: [],
      equModelCode: "",
      depreciationRate: null,
      expectedLife: null,
      equModelTemplateList: [],
      // 表单展示项
      tableLabel: [
        {
          label: "型号名称",
          param: "modelName",
          show: true
        },
        {
          label: "所属设备型号",
          param: "equipmentTypeName",
          show: true,
          notSearch: true
        }
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        width: 220,
        options: [
          {
            label: "操作",
            slotName: "operate"
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    // this.queryPageList();
  },
  mounted() {},
  methods: {
    updataTable1(e) {
      this.equipmentTypeCode = null;
      this.currentPage = 1;
      this.queryPageList(e);
    },
    // 查看详情
    queryDetail(row) {
      this.equModelTemplateList = row.equModelTemplateList || [];
      this.showDetailDialog = true;
    },
    queryDataBytype(data, node) {
      this.equipmentTypeCode = data;
      this.depreciationRate = node.depreciationRate;
      this.expectedLife = node.expectedLife;
      this.queryPageList(data);
    },
    updataTable(e) {
      this.queryPageList(e);
    },
    queryPageList(e) {
      console.log("queryPageList", e);
      this.loading = false;
      if(!this.equipmentTypeCode) this.equipmentTypeCode = e;
      var equipmentTypeCode = this.equipmentTypeCode || -1;
      var modelName = this.modelName;
      if (typeof e !== "object"&&e) {
        equipmentTypeCode = e;
      } else {
        modelName = e.modelName;
      }
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          modelName,
          equipmentTypeCode
        }
      };
      equipTemplateApi.queryPageList(param).then(res => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    handleEdit(row) {
      this.equModelCode = row.equModelCode;
      if (row.equModelCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.addActive = true;
        this.editActive = true;
      } else {
        this.editObject = {};
        this.addActive = true;
        this.editActive = false;
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.equModelCode) {
          return item.equModelCode;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach(a => {
        form.append("equModelCodeList", a);
      });
      equipModelApi.delList(form).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList(this.equipmentTypeCode);
          this.resetActionBar();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.editActive = false;
      this.showAddEditDialog = true;
      this.editObject = {
        equipmentTypeCode: this.equipmentTypeCode,
        depreciationRate: this.depreciationRate * 100,
        expectedLife: this.expectedLife
      };
    },
    // 修改
    edit(row) {
      if (row.equModelCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.editActive = false;
        this.showAddEditDialog = true;
      }
    },
    dels(data) {
      this.$confirm("此操作将永久删除该类型, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          let form = new FormData();
          form.append(
            "equModelCodeList",
            data?.equModelCode || this.equModelCode
          );
          equipModelApi.delList(form).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList(this.equipmentTypeCode);
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // table点击事件 在配置文件中 methods 传入事件
    clickButton(val) {
      console.log("表单事件", val);

      if (val.methods === "edit") {
        this.edit(val.row);
      } else if (val.methods === "del") {
        this.dels(val.row);
      } else if (val.methods === "detail") {
        this.queryDetail(val.row);
      }
    },
    closeDialog(e) {
      console.log(this.showAddEditDialog);
      if (this.showTreeAddEditDialog === true) {
        this.getTreeDataList();
        this.cancelDialog();
      }
      if (this.showAddEditDialog === true) {
        this.queryPageList(e);
        this.cancelDialog();
      }
    },
    cancelDialog() {
      // if (this.showAddEditDialog === true) {
      //   this.queryPageList();
      // }
      this.showAddEditDialog = false;
      this.showTreeAddEditDialog = false;
      this.showDetailDialog = false;
      this.nowChanceStau = "";
      this.titleText = "";
      this.editObject = {};
      this.addActive = true;
      this.delActive = false;
      this.editActive = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.searchbox {
  width: 100%;
  background-color: #00485a;
  display: flex;
}
::v-deep .select-box-content {
  flex-wrap: nowrap;
  .select-box-content-name {
    text-wrap: nowrap;
  }
}
::v-deep .el-table {
  .el-icon {
    color: #fff;
  }
  .el-table__expanded-cell {
    background: #4affda00;
  }
}
</style>
